﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PLCWpfTest.Converters
{
    public class StringToImageBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Value can not be null");
            }
            Uri uri = new Uri(value.ToString());
            BitmapImage bitmap = new BitmapImage(uri);
            ImageBrush imageBrush = new ImageBrush(bitmap);
            return imageBrush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Value can not be null");
            }
            ImageBrush imageBrush = (ImageBrush)value;
            BitmapImage bitmap = (BitmapImage)imageBrush.ImageSource;
            return bitmap.UriSource.AbsolutePath;
        }
    }
}
